package com.zyf.school.manager.controller;

import com.zyf.school.manager.domain.Fees;
import com.zyf.school.manager.service.FeesService;
import com.zyf.school.manager.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/fees/")
public class FeesController {

    @Autowired
    private FeesService feesService;

    List<Fees> feesList;
    Fees fees;


    private int totalCount;

    public void init(PageBean page){
        page.setTotalCount(feesService.count());
    }
    @RequestMapping("save")
    public String saveOrUpdate(HttpServletRequest request ,Fees fees){
        feesList = feesService.saveOrUpdate(fees);
        request.setAttribute("feesList",feesList);
        PageBean page = new PageBean();
        page.setCurrentPage(1);
        page.setStartIndex(0);
        page.setPageSize(10);
        this.init(page);
        request.setAttribute("page",page);
        return "/fees/template";
    }
    //分页查询
    @RequestMapping("template")
    public String template(HttpServletRequest request ,PageBean page){
        this.init(page);
        feesList = feesService.queryAll(page.getStartIndex(),page.getPageSize());
        request.setAttribute("feesList",feesList);
        request.setAttribute("page",page);
        return "/fees/template";
    }

    @RequestMapping("add")
    public String add(HttpServletRequest request){
        request.setAttribute("fees",new Fees());
        return "/fees/add";
    }

    @RequestMapping("edit")
    public String edit(HttpServletRequest request ,int id){
        fees = feesService.getFeesById(id);
        request.setAttribute("fees",fees);
        return "/fees/add";
    }
}
